import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.media.j3d.Canvas3D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import nba2kmodtool.filedesc.BaseIffFile;
import nba2kmodtool.filedesc.IffFactory;
import nba2kmodtool.filedesc.N2k3DModelPart;
import nba2kmodtool.filedesc.N2kModelRender;

public class Main3DFrameTest10 extends JFrame {

	private static int sel = 3;
	private static final int VERSION = 2009;

	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BaseIffFile iff = loadIff(new File("D:/My Documents/s001.iff"));
					sel = 11;
					//BaseIffFile iff = new BaseIffFile(new File("D:/My Documents/nbaball.iff"));
					//sel = 0;
					//BaseIffFile iff = new BaseIffFile(new File("F:/game/NBA 2K11/png0008.iff"));
					//BaseIffFile iff = new BaseIffFile(new File("F:/game/NBA 2K11/png547.iff"));
					//sel = 2;
					Main3DFrameTest10 objLoad = new Main3DFrameTest10(iff);
					objLoad.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(-1);
				}
			}
		});
	}

	public static BaseIffFile loadIff(File file) throws IOException {
		return IffFactory.readIffFile(file, VERSION);
	}

	public Main3DFrameTest10(BaseIffFile iff) {
		this.setTitle("N2K_x Iff 3D Frame Test");
		this.iff = iff;
		// Initialize the GUI components
		initComponents();
		// Create Canvas3D and SimpleUniverse; add canvas to drawing panel
		loader = new N2kModelRender();
		Canvas3D c = loader.createCanvas();
		drawingPanel.add(c, java.awt.BorderLayout.CENTER);
	}

	public void load() {
		try {
			//N2kTextureFile texture = iff.readPackageFile(0).getTextures()[0];
			//N2kTextureFile texture = iff.readTextureFile(5);
			//image = iff.readN2KTexture(texture);
			//ishow.setIcon(new ImageIcon(image));
			//ishow.updateUI();
			N2k3DModelPart[] pag = iff.readPackageFile(sel).getModels();
			N2k3DModelPart md = pag[0];
			loader.loadModelDatas(iff, pag);
			//loader.loadModelData(iff, md);
			drawingPanel.updateUI();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void initComponents() {
		drawingPanel = new javax.swing.JPanel();
		JPanel tool = new JPanel();
		tool.setLayout(new BorderLayout());
		JButton button = new JButton("Load");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				load();
			}
		});
		tool.add(button, BorderLayout.NORTH);
		button = new JButton("STOP");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loader.rotationModels(false);
			}
		});
		tool.add(button, BorderLayout.CENTER);
		add(tool, BorderLayout.NORTH);

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("ObjLoad");
		drawingPanel.setLayout(new java.awt.BorderLayout());

		//image = new BufferedImage(400, 400, Image.SCALE_SMOOTH);
		//ishow = new JLabel(new ImageIcon(image));
		//drawingPanel.add(ishow, BorderLayout.CENTER);
		drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
		getContentPane().add(drawingPanel, java.awt.BorderLayout.CENTER);

		pack();
	}

	private JPanel drawingPanel;
	private BaseIffFile iff;
	private N2kModelRender loader;
	private Image image;
	private JLabel ishow;
}
